package com.sajus.web.billing.admin.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import javax.naming.NamingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sajus.billing.client.delegate.BusinessDelegateFactory;
import com.sajus.billing.client.dto.OfferingDTO;
import com.sajus.billing.client.dto.RateMappingDTO;
import com.sajus.billing.client.dto.ServiceDTO;
import com.sajus.billing.client.dto.ServiceTypeDTO;

/**
 * Acts as a Struts 2 controller that responds
 * to a user action by setting the value
 * of the Message model class, and returns a String 
 * result.
 * @author Bruce Phillips
 *
 */
public class RateMappingAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	/**
	 * The model class that stores the message
	 * to display in the view.
	 */
	private List<ServiceDTO> services;
	
	private ServiceDTO service;
	
	
	/*
	 * Creates the MessageStore model object and 
	 * returns success.  The MessageStore model
	 * object will be available to the view.
	 * (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		
		System.out.println("Loading Rate Mapping Page" );
		
		if ( this.service != null ) {
			BusinessDelegateFactory.getInstance().getServiceTypeManager().saveService( this.service );
		}
		
		String paramValue = ServletActionContext.getRequest().getParameter("serviceId");
		if ( paramValue != null && !paramValue.equals( "" ) ){
			Integer id = new Integer( Integer.parseInt( paramValue ) );
			System.out.println("Value of Rate Mapping " + paramValue );
		
			this.service = BusinessDelegateFactory.getInstance().getServiceTypeManager().getService( id );
		} else {
			System.out.println( "Initialize Rate Mapping" );
			this.service = new ServiceDTO();
		}
		
		if( this.services == null || this.services.isEmpty() ){
			
			this.services = BusinessDelegateFactory.getInstance()
					.getServiceTypeManager().getAllServices();
			
		}
		
		return SUCCESS;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public List<ServiceDTO> getServices() {
		return services;
	}


	public void setServices(List<ServiceDTO> services) {
		this.services = services;
	}


	public ServiceDTO getService() {
		return service;
	}


	public void setService(ServiceDTO service) {
		this.service = service;
	}

	public List<OfferingDTO> getOfferings(){
		try {
			return BusinessDelegateFactory.getInstance().getServiceTypeManager().getOfferings();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

}
